package com.wushi.lenovo.asia5b.app_ui.pay.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.TextView;

import com.mvp.base.BaseAppCompatActivity;
import com.mvp.base.BaseSimpleFragment;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_event.OrderPayEvent;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TopupInfoBean;
import com.wushi.lenovo.asia5b.app_ui.other.activity.MainActivity;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.pay.PayResultDialog;
import com.wushi.lenovo.asia5b.app_ui.pay.fragment.PayInsCardFragment;
import com.wushi.lenovo.asia5b.app_ui.pay.fragment.PayInsFpxFragment;
import com.wushi.lenovo.asia5b.app_ui.pay.view.PayPriceInfoView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * Created by zhangyuncai on 2019/2/16.
 * 余额不足使用FPX/信用卡差额付款,todo容器页面
 */
public class PayInsVesselActivity extends BaseAppCompatActivity {
    @BindView(R.id.payprice)
    PayPriceInfoView payprice;
    @BindView(R.id.tv_pay_type)
    TextView tv_pay_type;
    public TopupInfoBean topupInfoBean;
    private boolean isFpx = true;//true/false : fpx/visa

    @Override
    protected int getLayoutId() {
        return R.layout.activity_payinsvessel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        topupInfoBean = (TopupInfoBean) getIntent().getSerializableExtra(CommonConstant.TopupInfoBean);
        isFpx = getIntent().getBooleanExtra("isFpx", true);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        Bundle bundle = new Bundle();
        Fragment fragment = null;
        if (!isFpx) {
            tv_pay_type.setText(R.string.orderpayment_paywithadd);
            fragment = BaseSimpleFragment.sNewStance(this, PayInsCardFragment.class.getName());
        } else {
            tv_pay_type.setText(R.string.pay_with_fpx);
            fragment = BaseSimpleFragment.sNewStance(this, PayInsFpxFragment.class.getName());
        }
        getSupportFragmentManager().beginTransaction().add(R.id.fl_pay_container, fragment).commit();
        payprice.setTopupInfoBean(topupInfoBean);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onOrderPayEvent(OrderPayEvent event) {
        if (event != null) {
            PayResultDialog dialog = new PayResultDialog(mActivity, event.resultModel, true);
            dialog.show();
            if (event.resultModel.isSuccess() || true) {
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (event.resultModel.isSuccess()) {
                            Intent intent = new Intent(mActivity, MainActivity.class);
                            mActivity.startActivity(intent);
                        }
                        finish();
                    }
                });
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
